package com.list;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

public class ListUtil {

    /**
     * 将多个参数转化为集合
     */
    public static <T> List<T> asList(T... args) {
        List<T> list = new ArrayList<>();
        if (!ObjectUtils.isEmpty(args)){
            list.addAll(Arrays.asList(args));
        }
        return list;
    }

    /**
     * 拼接
     */
    public static <T> String join(List<T> list, String mark) {
        return StringUtils.join(list, mark);
    }

    /**
     * 过滤
     */
    public static <T> List<T> filter(List<T> list, Filter<? super T> filter) {
        List<T> result = new ArrayList<>();
        for (T t : list) {
            if (filter.flag(t)){
                result.add(t);
            }
        }
        return result;
    }

    /**
     * 排序
     */
    public static <T> List<T> sort(List<T> list, Comparator<T> comparator) {
        list.sort(comparator);
        return list;
    }

    /**
     * 获取属性集合
     */
    public static <T,V> List<V> getColumn(List<T> list, Column<T,V> column) {
        List<V> columnList = new ArrayList<>();
        for (T t : list) {
            columnList.add(column.getColumn(t));
        }
        return columnList;
    }

    /**
     * 去重
     */
    public static <T> List<T> distinct(List<T> list, Comparator<T> comparator) {
        Set<T> userInfoSet = new TreeSet<>(comparator);
        userInfoSet.addAll(list);
        return new ArrayList<>(userInfoSet);
    }
    
}


